media-query-parser
Quickfire examples
import { toAST } from 'media-query-parser'
console.log(toAST('(max-width: 768px)'))
console.log(toAST('print, (not (color))'))
console.log(toAST('@media screen { body { background: #000 } }'))
console.log(toAST('(100px < width < 200px)'))
console.log(toAST('(4/3 <= aspect-ratio <= 16/9)'))
console.log(toAST('clearly this is not a valid media query'))
{
console.log(toAST('(max-width: 768px) and screen'))
console.log(toAST('screen and (max-width: 768px) or (hover)'))
}
Considerations & Caveats
This library does:
- remove extra layers from unnecessary parentheses
(((((max-width: 768px)))))
- parses units, numbers and other values to the spec
- handle unusual whitespace anywhere that the spec allows it
- contain many a unit test
This library does not:
- sanity check the actual media features or their types
(max-power: infinite)
is as valid as (hover: none)
- normalize the media query features (e.g.
(max-width: -100px)
is always
false
)
Note that:
- Handling individual media features is being developed in a separate project:
media-query-fns
Installation
npm install media-query-parser --save
Alternatively, there are also client web builds available:
<script src="https://unpkg.com/media-query-parser/dist/media-query-parser.umd.js"></script>
License
MIT